package com.ktjy.ktmall.service.impl;

import com.ktjy.ktmall.dao.TUserDao;
import com.ktjy.ktmall.entity.TUser;
import com.ktjy.ktmall.service.TUserService;

import java.util.List;


public class TUserImpl implements TUserService {
    private TUserDao userDao= new com.ktjy.ktmall.dao.impl.TUserImpl();

    @Override
    public List<TUser> getAllUsers() {
        return userDao.getUserList();
    }

    @Override
    public TUser getUserById(long id) {
        return userDao.getUserById(id);
    }

    @Override
    public TUser getUserByLoginName(String loginName) {
        return userDao.getUserByLoginName(loginName);
    }

    @Override
    public boolean addUser(TUser user) {
        if (user == null) {
            return false;
        }
        userDao.addUser(user);
        return true;
    }

    @Override
    public void updateUser(TUser user) {
        if (user == null || user.getId() == 0) {
            throw new IllegalArgumentException("User is null or ID is invalid");
        }
        userDao.updateUser(user);
    }

    @Override
    public void deleteUser(long id) {
        if (id <= 0) {
            throw new IllegalArgumentException("Invalid user ID");
        }
        userDao.deleteUser(id);
    }

    @Override
    public TUser login(String loginName, String password) {
        //首先验证该用户表当中是否有这个用户
        TUser user = getUserByLoginName(loginName);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        } else {
            return null;
        }
    }
}
